#!/usr/bin/env bash

if [ -z "$1" ]; then
  echo "请提供类型名称作为参数"
  exit 1
fi

DB_NAME="$1"
SORTED_OUTPUT_FILE=~/Downloads/"${DB_NAME}"_sort.csv
SORTED_WF_OUTPUT_FILE=~/Downloads/wf_"${DB_NAME}"_sort.csv
DIFF_OUTPUT_FILE=~/Downloads/diff_result.csv

#获取所有数据库名
DATABASES=$(mysql --defaults-group-suffix=.lk.admin -N -B -e "SHOW DATABASES LIKE 'lk_%'")

# 创建一个临时文件
TMP_FILE=$(mktemp)

#查询工厂的数据
if [ "$DB_NAME" == "product" ]; then
  # 如果参数是 "product"，执行产品查询
  for DB in $DATABASES; do
    CODE=${DB:3}
    mysql --defaults-group-suffix=.lk.admin -N -B "$DB" -e "
    SELECT '${CODE}', barcode,
                      CASE status
                      WHEN '1' THEN 'VERIFY'
                      WHEN '2' THEN 'TOWASH'
                      WHEN '3' THEN 'WASHING'
                      WHEN '4' THEN 'WASHED'
                      WHEN '5' THEN 'TOINSPECT'
                      WHEN '6' THEN 'ATTACH'
                      WHEN '7' THEN 'TOWRAP'
                      WHEN '8' THEN 'WRAPPED'
                      WHEN '9' THEN 'PLACED'
                      WHEN '10' THEN 'TOPICK'
                      WHEN '11' THEN 'PICKED'
                      WHEN '12' THEN 'PACKED'
                      WHEN '13' THEN 'SHIPPED'
                      ELSE 'Unknown'
                      END AS status_description FROM opr_${DB_NAME} where flag & 512 != 512 and status != '1' and companion_id is null"
  done > "$TMP_FILE"
else
  # 否则，执行出入厂单查询
  for DB in $DATABASES; do
    CODE=${DB:3}
#    mysql --defaults-group-suffix=.lk.admin -N -B "$DB" -e "SELECT '${CODE}', ch_${DB_NAME}_no FROM opr_${DB_NAME} where status = '7' and flag & 512 != 512"
#    mysql --defaults-group-suffix=.lk.admin -N -B "$DB" -e "SELECT express_no FROM opr_${DB_NAME} where status = '7' and flag & 512 != 512"
    mysql --defaults-group-suffix=.lk.admin -N -B "$DB" -e "SELECT '${CODE}',express_no FROM opr_shipment ost
                                                            WHERE ost.status ='6'
                                                            and ost.flag & 512 != 512
                                                            and not exists (
                                                            	select 1 from opr_product
                                                            	where shipment_id = ost.id
                                                            	and flag & 512 != 512
                                                            	and status <> '12'
                                                            )"
  done > "$TMP_FILE"
fi


# 排序临时文件并将结果保存到最终的排序文件
sort "$TMP_FILE" > "$SORTED_OUTPUT_FILE"

# 清理临时文件
rm "$TMP_FILE"
#
#echo "排序完成并保存到 $SORTED_OUTPUT_FILE"
#
## 重新创建临时文件
#TMP_FILE=$(mktemp)
#
##查询waterfall的数据
#if [ "$DB_NAME" == "product" ]; then
#  # 如果参数是 "product"，执行产品查询
#  for DB in $DATABASES; do
#    CODE=${DB:3}
#    mysql --defaults-group-suffix=.lk.admin -N -B "$DB" -e "SELECT '${CODE}', barcode FROM opr_${DB_NAME} where status = '9'"
#  done > "$TMP_FILE"
#else
#  # 否则，执行出入厂单查询
#  for DB in $DATABASES; do
#    CODE=${DB:3}
#    mysql --defaults-group-suffix=.lk.admin -N -B "$DB" -e "SELECT '${CODE}', some_other_column FROM opr_${DB_NAME}"
#  done > "$TMP_FILE"
#fi
## 排序临时文件并将结果保存到最终的排序文件
#sort "$TMP_FILE" > "$SORTED_WF_OUTPUT_FILE"
#
## 清理临时文件
#rm "$TMP_FILE"
#
#echo "排序完成并保存到 $SORTED_WF_OUTPUT_FILE"
#
## 使用diff比对两个文件，并将结果保存到DIFF_OUTPUT_FILE
#diff "$SORTED_OUTPUT_FILE" "$SORTED_WF_OUTPUT_FILE" > "$DIFF_OUTPUT_FILE"
#
#echo "diff完成并保存到 $DIFF_OUTPUT_FILE"
#
## 使用mutt发送邮件：
#mutt -s "${DB_NAME}工厂和管理端对账结果" mengjingfeng@dajingxiapp.com < "$DIFF_OUTPUT_FILE"
#
#echo "邮件发送成功"



